package beyjava.view;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.Vector;

import beyjava.model.Activity;
import beyjava.model.Person;
import beyjava.model.Resource;
import beyjava.model.Task;
import beyjava.util.Common;
import beyjava.util.IdGenerator;
import beyjava.util.Validator;

/**
 * @author BEYJAVA
 * task menu view class
 */
public class TaskMenu {
	 Scanner sc;
     /**
     * constructor of taskMenu class
     */
    public TaskMenu(){
   	  sc = new Scanner(System.in);
     }
     /**
     * @return integer choose from user to get task operation
     * method to list available Task operations
     */
    public int chooseMainMenu(){// maindeki show menu;
    	System.out.println("1- Add Task");
		System.out.println("2- Remove Task");
		System.out.println("3- Update Task");
		System.out.println("4- Assign Resource to Task");
		System.out.println("5- Unassign Resource From Task");
		System.out.println("0- Return Previous Menu");//bu activity menuye doncek
   	    return Common.getChoose(0, 5);
   	}
     /**
     * @param actOfTask
     * @return addedTask property of new Task which is wanted to add.
     * @throws IllegalArgumentException,
     * @throws InputMismatchException
     * method to get properties of new added task from user
     */
    public Task addTaskMenu(Activity actOfTask){
	     Task addedTask = null;
	     Integer taskID = IdGenerator.generateIntId();
		 String taskDesc;
		 Date taskStartDate;
		 Integer taskHours;
		 try{
			 System.out.println("Please Fill the Required Field For Adding a Task.");
			 System.out.println("Task Description :");
			 taskDesc = sc.next();
			 Validator.textHasContent(taskDesc);
			 System.out.println("Task Start Date(Date format must be in this format dd-MM-yyyy) :");
			 taskStartDate  = Common.getDate("dd-MM-yyyy");
			 if(Common.dateComprasion(actOfTask.getActStartDate(),taskStartDate)){
				 System.out.println("the task start date  should be bigger than  activity start date");
				 return null;
			 }
			 System.out.println("Task Hours :");
			 taskHours=Common.getInteger();
			 addedTask = new Task(taskID, taskDesc, taskStartDate, taskHours, actOfTask);
		 }
		 catch(IllegalArgumentException ex){
			System.out.println(ex.getMessage());
		 }catch (InputMismatchException e){
			System.out.println("Input must be integer number.Please enter again.");
		 }
		 return addedTask;
	}
     /**
     * @param tasks
     * @return task which is wanted to remove from user
     * method to list available tasks and get chose of user to remove operation
     */
    public Task removeTaskMenu(Vector<Task> tasks){
    	 if(tasks==null || tasks.size()<1){
    		 System.out.println("There is no task in this activity.");
    		 return null;
           }
    	 return selectTaskMenu(tasks, "remove");
     }
     /**
     * @param tasks
     * @return task which is wanted to update from user
     * method to list available tasks and get chose of user to update operation
     */
    public Task updatedTaskMenu(Vector<Task> tasks){
    	 if(tasks==null || tasks.size()<1){
    		 System.out.println("There is no task in this activity.");
    		 return null;
    	 }
    	 return selectTaskMenu(tasks, "update");
     }  	 
	 /**
	 * @param updatedTask
	 * @return newTask which has new properties of updated task
	 * @throws Exception
	 * method to get update task properties from user
	 */
	public Task updatedTaskMenuSelection(Task updatedTask){
    	 String DATE_FORMAT = "dd-MM-yyyy";
     	 SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
    	 Task newTask=new Task(updatedTask.getTaskNumber(),updatedTask.getTaskDesc(),updatedTask.getTaskStartDate(),
    			 updatedTask.getTaskHours(),updatedTask.getActOfTask());
    	 int act=0;
    	 String y = "";
    	 
	    	 System.out.println("1- Task Description : " + updatedTask.getTaskDesc().toUpperCase());
	 		 System.out.println("2- Task StartDate : " + sdf.format(updatedTask.getTaskStartDate()));
	 		 System.out.println("3- Task Hours : " + updatedTask.getTaskHours());
	 		 System.out.println("Please choose one of the task operations.");
	 		try{
				act = Common.getChoose(0, 3);
				switch(act){
					case 1: System.out.println("Please enter new Task Description :");
						y = sc.next();
						Validator.textHasContent(y);
						newTask.setTaskDesc(y);
						break;
					case 2: System.out.println("Please enter new Task StartDate :");
						Date date = Common.getDate("dd-MM-yyyy");
						 if(Common.dateComprasion(updatedTask.getActOfTask().getActStartDate(),date)){
							 System.out.println("the task start date  should be bigger than  activity start date");
							 return null;
						 }
						newTask.setTaskStartDate(date);
						break;
					case 3: System.out.println("Please enter new Task Hours :");
						act = Common.getInteger();
						newTask.setTaskHours(act);
						break;
					default:
						break;
				}
	 		}
	 		catch(Exception ex){
				ex.getMessage();
			}   	 
    	 return newTask;
     }
     /**
     * @param tasks
     * @return task which is selected by user to assigns resource
     * method to list available tasks and get chose of user to assigns resource operation
     */
    public Task assignResourceToTask(Vector<Task> tasks){
    	 
    	 if(tasks==null || tasks.size()<1){
    		 System.out.println("There is no task in this activity.");
    	 	return null;
    	 }
    		return selectTaskMenu(tasks, "assign resource");	 
     }
     /**
     * @param tasks
     * @return task which is selected by user to unassigns resource
     * method to list available tasks and get chose of user to unassigns resource operation
     */
    public Task unassignedResourceTask(Vector<Task> tasks){
    	 if(tasks==null || tasks.size()<1){
    		 System.out.println("There is no task in this activity.");
    	 	return null;
    	 }
    	 return selectTaskMenu(tasks, "unassign resource");	 
     }
     /**
     * @param people
     * @return resource which is selected by user to assigns to task
     * method to list available resources and get chose of user to assigns to task operation
     */
    public Resource addResourceMenu(Vector<Person> people){
    	 if(people.size() < 1 || people == null){
    		System.out.println("There is no resource. Please add a resource");
    		return null;
    	 }
		 for(int i=0;i<people.size();i++){
			 System.out.print((i+1)+"- ");
			 System.out.println("ID:"+people.get(i).getResourceID()+" ,Name:"+people.get(i).getPersonName()+" ,Resource Type:"
					 +people.get(i).getResourceType());	 
		 }
		 System.out.println("Please choose one of the resources to assign operation.");
		 int resourceNumber = Common.getChoose(1, people.size());
		 return people.get(resourceNumber-1);
     }
     /**
     * @param resourceTask
     * @param resource
     * @return resource which is selected by user to unassigns from task
     * method to list available resources and get chose of user to unassigns from task operation
     */
    public Resource removeResourceFromTaskMenu(Task resourceTask ,Vector<Person> resource){
	    if(resourceTask == null){
	    	System.out.println("There is no task. Please add a task");
	    	return null;
	    }
    	if(  resource == null || resource.size() < 1l){
		   System.out.println("There is no resource for this task. Please add a resource or select different task");
		   return null;
	    }
	  
	   System.out.println("The task's resource is  as follow.Please choose a resource to unassign from the task");
	   for(int i = 0; i < resource.size(); i++){
		   Person p = resource.get(i);
		   System.out.print((i+1)+"- ");
			 System.out.println("ID:"+p.getResourceID()+" ,Name:"+p.getPersonName()+"Resource Type:"+p.getResourceType());
	   }
	   System.out.println("Please choose one of the resources to unassign operation.");
	    int resourceNumber = Common.getChoose(1, resource.size());
	    return resource.get(resourceNumber-1);
    }
     /**
     * @param tasks
     * @param action
     * @return task which is chosen from user
     * method to list available tasks and get chose of user to wanted operation
     */
    public Task selectTaskMenu(Vector<Task> tasks,String action){
     	   System.out.println("Tasks of Activity:");
    		for(int i = 0;i < tasks.size();i++){
    			System.out.println((i+1)+"- "+tasks.get(i).getTaskDesc());
    		}
    		System.out.println("Please Write One Of The Task's Number To "+action+":");
    		int choose = Common.getChoose(1, tasks.size());
    		return tasks.get(choose-1);
     	   
        }
}
